package com.game.sprite;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.game.animation.AnimationObject;
import com.game.basic.BasicView;
import com.game.config.Config;
import com.game.utils.Tools;


/**
 * 判断奇偶数可用 value & 1 == 0 偶数  else  奇数
 * 或 | 二进制位有一个 1则为1  ex:9|5   00001001|00000101 = 00001101
 * 异或 ^ 二进制位不一样则为1  ex:9|5   00001001^00000101 = 00001100   
 * 左移位 <<  最高位抛弃 最低位补0   右移一位相当于乘以2 移动n位相当于乘以2的n次方
 * 右移位 >> 全部右移,最左边加上符号位   右移一位相当于除以2 移动n位相当于除以2的n次方
 */
/**
 * 精灵类  继承 AnimationObject
 * @author Administrator
 *
 */
public abstract class BasicSprite extends AnimationObject{
//	protected Paint paint = Tools.createNewPaint();
	
	protected float collisionPerc=0.7f;
	
	protected int[] counter;
	
	protected boolean drawCollectionRegion = true;
	
	protected boolean visiable = true;
	
	protected boolean touchAble = true;
	
	public BasicSprite(){
	}
	
	/**
	 * 精灵的绘制方法,首先调用父类的绘制方法
	 */
	public void draw(Canvas canvas){
		if(visiable){
			super.draw(canvas);
			if(Config.DRAW_REGION&&drawCollectionRegion){
				drawCollectionRegion(canvas);
			}
		}
	}
	
	
	/**
	 * 绘制碰撞区域
	 * @param canvas
	 */
	protected void drawCollectionRegion(Canvas canvas){
		int color = paint.getColor();
		int alpha = paint.getAlpha();
		paint.setColor(Color.BLUE);
		paint.setAlpha(80);
		
		drawRegion(canvas,paint,getCollisionRegion());
		
		paint.setColor(color);
		paint.setAlpha(alpha);
	}
	
	public boolean count(int index,float max){
		counter[index] = (int) ((counter[index] + 1) % max);
		return counter[index]==0;
	}
	
	/**
	 * 获取碰撞区域
	 * @return
	 */
	public  float[] getCollisionRegion(){
		if(drawAnimation){
			return getCollisionRegionWithAnimation(true);
		}else{
			return getCollisionRegionWithAnimation(false);
		}
		
	}
	
	/**
	 * 获取碰撞区域
	 * 受animation影响
	 * @return
	 */
	public float[] getCollisionRegionWithAnimation(boolean withAnimation){
		float[] result = new float[4];
		float width = getWidth()*collisionPerc;
		float height = getHeight()*collisionPerc;
		float centerx = getCenterX();
		float centery = getCenterY();
		if(withAnimation&&animation!=null){
			centerx=centerx+animationFix.getTransx();
			centery=centery+animationFix.getTransy();
			width=width*(1+animationFix.getScalex());
			height=height*(1+animationFix.getScaley());
		}
		result[0]=centerx-width/2;
		result[1]=centery-height/2;
		result[2]=centerx+width/2;
		result[3]=centery+height/2;
		return result;
	}
	
	
	/**
	 * 判断一点是否在碰撞区域内
	 * @param points
	 * @return
	 */
	public boolean isCollesion(float[] points){
		boolean flag=BasicView.isTouchInRegion(points[0],points[1], getCollisionRegion());
		return flag;
	}
	
	/**
	 * 判断是否与当前精灵碰撞
	 * @param sp1
	 * @return
	 */
	public boolean isCollsionWith(BasicSprite sp1){
		return isCollsionWith(sp1.getCollisionRegion());
	}
	
	/**
	 * 判断是否与当前精灵碰撞
	 * @param sp1
	 * @return
	 */
	public boolean isCollsionWith(float[] region){
		return isCollsionWith(region,this.getCollisionRegion());
	}
	
	/**
	 * 判断两个精灵是否发送碰撞
	 * @param sp1
	 * @param sp2
	 * @return
	 */
	public static boolean isCollsionWith(BasicSprite sp1,BasicSprite sp2){
		return isCollsionWith(sp1.getCollisionRegion(),sp2.getCollisionRegion());
	}
	
	/**
	 * 判断碰撞 矩形碰撞
	 * @param region1
	 * @param region2
	 * @return
	 */
	public static boolean isCollsionWith(float[] region1,float[] region2){
		float sx1=region1[0];
		float sy1=region1[1];
		float ex1=region1[2];
		float ey1=region1[3];
		float w1=ex1-sx1;
		float h1=ey1-sy1;
		float sx2=region2[0];
		float sy2=region2[1];
		float ex2=region2[2];
		float ey2=region2[3];
		float w2=ex2-sx2;
		float h2=ey2-sy2;
		//1 在 2的左边
		if (sx1 >= sx2 && sx1 >= ex2) {
			return false;
		//1在2的右边
		} else if (sx1 <= sx2 && ex1 <= sx2) {
			return false;
		//1在2的上边
		} else if (sy1 >= sy2 && sy1 >= ey2) {
			return false;
		//1在2的下边
		} else if (sy1 <= sy2 && ey1 <= sy2) {
			return false;
		}
		//碰撞了
		return true;
	}
	
	
	public void show(){
		visiable=true;
	}
	
	public void enableTouch(){
		touchAble=true;
	}
	
	public void showAndEnableTouch(){
		show();
		enableTouch();
	}
	
	public void hide(){
		visiable=false;
	}
	
	public void hideAndDisableTouch(){
		hide();
		disableTouch();
	}
	
	public void disableTouch(){
		touchAble=false;
	}
	
	/**
	 * 把指定图片以精灵的中心点绘制
	 * @param canvas
	 * @param bitmap
	 */
	public void drawCenterPoint(Canvas canvas ,Bitmap bitmap){
		drawCenterPointFix(canvas,bitmap,0,0);
	}
	
	public void drawCenterPointFix(Canvas canvas ,Bitmap bitmap,float fixX,float fixY){
		float[] center=getCenter();
		float width = bitmap.getWidth();
		float height = bitmap.getHeight();
		canvas.drawBitmap(bitmap, center[0]-width/2+fixX,center[1]-height/2+fixY, paint);
	}

	public float getCollisionPerc() {
		return collisionPerc;
	}

	public void setCollisionPerc(float collisionPerc) {
		this.collisionPerc = collisionPerc;
	}

	public boolean isVisiable() {
		return visiable;
	}

	public boolean isTouchAble() {
		return touchAble;
	}

	public void setVisiable(boolean visiable) {
		this.visiable = visiable;
	}

	public void setTouchAble(boolean touchAble) {
		this.touchAble = touchAble;
	}
	
	public boolean isTouchInRegion(float touchX,float touchY){
		return BasicView.isTouchInRegion(touchX,touchY ,getCollisionRegion());
	}
}